package com.itheima.jiexi;

import com.itheima.domain.Singer;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class Demo01 {

    public static void main(String[] args) throws DocumentException {
        //创建 集合
        ArrayList<Singer> list = new ArrayList<>();
        //获取解析对象
        SAXReader reader = new SAXReader();
        //获取文档对象
        Document document = reader.read("day18\\xml\\singers.xml");
        //获取根标签
        Element rootElement = document.getRootElement();
        //获取根标签的子标签们
        List<Element> singers = rootElement.elements("singer");

        //遍历子标签
        for (Element singerElement : singers) {
            //获取子标签的指定名称的子标签
            Element nameElement = singerElement.element("name");
            //获取子标签的指定名称的子标签的值
            String name = nameElement.getText();
            //获取子标签的指定名称的子标签
            Element ageElement = singerElement.element("age");
            //获取子标签的指定名称的子标签的值
            String ageStr = ageElement.getText();
            //获取子标签的指定名称的子标签
            Element addressElement = singerElement.element("address");
            //获取子标签的指定名称的子标签的值
            String address = addressElement.getText();
            //获取标签的属性对象
            Attribute idAttr = singerElement.attribute("id");
            //属性对象  获取属性值
            String id = idAttr.getValue();
            //封装数据
            Singer singer = new Singer(id, name, Integer.parseInt(ageStr), address);
            //插入集合
            list.add(singer);
        }

        //遍历集合
        list.forEach(s-> System.out.println(s));



    }
}
